package com.ruoyi.performance.controller;

import com.alibaba.fastjson.JSON;
import com.mysql.cj.xdevapi.JsonParser;
import com.mysql.cj.xdevapi.Result;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.performance.domain.PerfGroup2User;
import com.ruoyi.performance.service.PerfGroup2UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@RestController //绩效权限组成员列表
@RequestMapping("perfgroupuser")
public class PerfGroup2UserController extends BaseController {
    @Autowired
    PerfGroup2UserService perfGroup2UserService;

    @GetMapping("get/{id}")
    public R list(@PathVariable Long id){
        return jacResult(perfGroup2UserService.selectPerfGroup2UserById(id));
    }

    @GetMapping("getc/{Companycode}")
    public R list(@PathVariable("Companycode") String Companycode){
        startPage();
        return result(perfGroup2UserService.selectPerfGroup2UserByCompanycode(Companycode));
    }

    @RequestMapping("get/list")
    public R list(PerfGroup2User perfGroup2User){
        startPage();
        return result(perfGroup2UserService.selectPerfGroup2UserList(perfGroup2User));
    }


//    @GetMapping("get/{groupcode}")
//    public R list(@PathVariable String groupcode){
//        return jacResult(perfGroup2UserService.selectByGroupcode(groupcode));
//    }

//    @GetMapping("get/{usercode}")
//    public R list(@PathVariable String usercode){
//        return jacResult(perfGroup2UserService.selectByUsercode(usercode));
//    }

    @PostMapping("save")
    public R PlaceOrder( @RequestBody List<PerfGroup2User> perfGroup2Users){
        List<String> answers = new ArrayList<>();
        System.out.println(JSON.toJSONString(perfGroup2Users));
        for (PerfGroup2User obj : perfGroup2Users) {
            System.out.println(JSON.toJSONString(obj));
            perfGroup2UserService.insertPerfGroup2User(obj);
        }
        return R.ok();

//        JSONArray jsonArray = jsonObject.getJSONArray("initTree");
//        System.out.println(perfGroup2User);
    }

//    @PostMapping("save")
//    public R addSave(@RequestBody PerfGroup2User perfGroup2User){
////        Result result = new Result();
////        List<Integer> ids = new ArrayList<>();
////        List<String> answers = new ArrayList<>();
////        PerfGroupUser perfGroup = new PerfGroupUser();
////        List<PerfGroupUser>  perfGroupList = perfGroup2User.getList();
//////        JsonParser jsonParser = new JsonParser();
//////        JSONObject jsonObject = jsonParser.parse(json.toString()).getAsJsonObject();
//////        Map<String,Object> maps = new TreeMap<>();
////        System.out.println("这是我的测试数据" + perfGroup2User);
//        return toAjax(perfGroup2UserService.insertPerfGroup2User(perfGroup2User));
//    }

    @PostMapping("update")
    public R EditSave(@RequestBody List<PerfGroup2User>  perfGroup2Users){
        List<String> answers = new ArrayList<>();
        System.out.println(JSON.toJSONString(perfGroup2Users));
        for (PerfGroup2User pgu : perfGroup2Users){
            System.out.println(JSON.toJSONString(pgu));
            String  groupcode = pgu.getGroupcode();
            System.out.println(groupcode);
            perfGroup2UserService.deletePerfGroup2UserByGroupcode(groupcode);
           // perfGroup2UserService.insertPerfGroup2User(pgu);
            //String gc = JSON.toJSONString(pgu).getgroupcode();
            //System.out.println(gc);
            //perfGroup2UserService.updatePerfGroup2User(pgu);
        }
        for (PerfGroup2User pgu : perfGroup2Users){

            perfGroup2UserService.insertPerfGroup2User(pgu);

        }
        return R.ok();
    }

    @PostMapping("remove")
    public R remove(String id){
        return toAjax(perfGroup2UserService.deletePerfGroup2UserById(id));
    }

}
